import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/utils/string_util.dart';

class AvatarView extends StatelessWidget {
  static const String DEFAULT_AVATAR = 'images/avatar_default.png';
  final String url;

  AvatarView(this.url);

  static const double defaultSize = 100;

  @override
  Widget build(BuildContext context) {
    return _getImageItem(context, url);
  }

  _getImageItem(context, url) {
    if (StringUtil.isEmpty(url)) {
      return Image.asset(
        DEFAULT_AVATAR,
        width: defaultSize,
        height: defaultSize,
      );
    }
    try {
      return GestureDetector(
        onTap: () {
          print(url);
        },
        child: CachedNetworkImage(
          width: defaultSize,
          height: defaultSize,
          fit: BoxFit.cover,
          imageUrl: url,
          httpHeaders: DioHttpClient.headers,
          placeholder: (context, url) => Image.asset(
            DEFAULT_AVATAR,
            width: defaultSize,
            height: defaultSize,
          ),
          errorWidget: (context, url, error) {
            print(error);
            return Image.asset(
              DEFAULT_AVATAR,
              width: defaultSize,
              height: defaultSize,
            );
          },
        ),
      );
    } catch (e) {
      print(e);
      return Image.asset(
        DEFAULT_AVATAR,
        width: defaultSize,
        height: defaultSize,
      );
    }
  }
}
